package com.igeekhome.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeekhome.biz.WorkOrderService;
import com.igeekhome.pojo.WorkOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;


import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 工作订单 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-03-16
 */
@Controller
@RequestMapping("/workOrder")
public class WorkOrderController {
    @Autowired
    WorkOrderService workOrderService;

    @RequestMapping("index")//全部工单
    public String index(Model model, @RequestParam("current") Integer current, @RequestParam("size") Integer size, @RequestParam("str1")String str1, @RequestParam("str2")String str2){
        QueryWrapper queryWrapper = new QueryWrapper();
        if(!str2.equals("null")){
            queryWrapper.eq(str1,str2);
        }
        initworkorderservice(model,current,size,queryWrapper);
        model.addAttribute("str1",str1);
        model.addAttribute("str2",str2);
        return "workorder";
    }

    @RequestMapping("index2")//待处理的工单
    public String index2(Model model, @RequestParam("current") Integer current, @RequestParam("size") Integer size){

        QueryWrapper<WorkOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state","待处理");
        initworkorderservice(model,current,size,queryWrapper);
        model.addAttribute("current",current);
        return "undealworkorder";
    }

    @RequestMapping("del")//全部工单删除工单
    public String del(Model model, WorkOrder workOrder, @RequestParam("current") Integer current, @RequestParam("size") Integer size,@RequestParam("str1")String str1, @RequestParam("str2")String str2){
        workOrderService.removeById(workOrder.getId());
        QueryWrapper queryWrapper = new QueryWrapper();
        if(!str2.equals("null")){
            queryWrapper.eq(str1,str2);
        }
        model.addAttribute("str1",str1);
        model.addAttribute("str2",str2);
        initworkorderservice(model,current,size,queryWrapper);
        return "workorder";
    }

    @RequestMapping("update")//全部工单里面修改工单
    public String update(Model model, WorkOrder workOrder, @RequestParam("current") Integer current, @RequestParam("size") Integer size,@RequestParam("str1")String str1, @RequestParam("str2")String str2){
        WorkOrder wo = workOrderService.getById(workOrder.getId());
        model.addAttribute("wo",wo);
        model.addAttribute("current",current);
        model.addAttribute("size",size);
        model.addAttribute("str1",str1);
        model.addAttribute("str2",str2);
        model.addAttribute("main","工单管理页面");
        return "updateworkorder";
    }
    @RequestMapping("updcom")//修改工单页面
    public String updcom(WorkOrder workOrder, Model model,@RequestParam("current") Integer current, @RequestParam("size") Integer size,@RequestParam("str1")String str1, @RequestParam("str2")String str2){
        System.out.println("能成功进入");
        WorkOrder wo = workOrderService.getById(workOrder.getId());
        wo.setTitle(workOrder.getTitle());
        wo.setCclist(workOrder.getCclist());
        wo.setState(workOrder.getState());
        wo.setChannel(workOrder.getChannel());
        wo.setPriority(workOrder.getPriority());
        //wo.setCreatetime(workOrder.getCreatetime());
        workOrderService.updateById(wo);
        QueryWrapper queryWrapper = new QueryWrapper();
        if(!str2.equals("null")){
            queryWrapper.eq(str1,str2);
        }
        model.addAttribute("str1",str1);
        model.addAttribute("str2",str2);
        initworkorderservice(model,current,size,queryWrapper);
        return "workorder";
    }

    @RequestMapping("check")//查看未处理工单详情
    public String check(WorkOrder workOrder, Model model,@RequestParam("current") Integer current, @RequestParam("size") Integer size){
        WorkOrder wo = workOrderService.getById(workOrder);
        model.addAttribute("wo",wo);
        model.addAttribute("current",current);
        model.addAttribute("main","工单管理页面");
        return "detailworkorder";
    }
    @RequestMapping("deal")//分配未分配的订单
    public String deal(WorkOrder workOrder, Model model,@RequestParam("current") Integer current, @RequestParam("size") Integer size){
        WorkOrder wo = workOrderService.getById(workOrder);
        wo.setState("处理中");
        workOrderService.updateById(wo);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("state","待处理");
        initworkorderservice(model,current,size,queryWrapper);

        model.addAttribute("current",current);
        return "undealworkorder";
    }
    @RequestMapping("create")//创建订单界面
    public String create(Model model){
        model.addAttribute("main","工单管理页面");
        return "createworkorder";
    }
    @RequestMapping("createcom")//点击创建成功
    public String createcom(WorkOrder workOrder, Model model){
        LocalDateTime localDateTime = LocalDateTime.now();
        model.addAttribute("localDateTime",localDateTime);
        workOrder.setCreatetime(localDateTime);
        workOrderService.save(workOrder);
        model.addAttribute("main","工单管理页面");
        return "createworkorder";
    }
    @RequestMapping("search")//查询订单界面
    public String search(@RequestParam("select")String select, Model model,@RequestParam("current") Integer current, @RequestParam("size") Integer size){

        QueryWrapper<WorkOrder> queryWrapper = new QueryWrapper<>();
        if (select !="") {
            if (select.equals("高") || select.equals("中") || select.equals("低") || select.equals("紧急")) {
                model.addAttribute("str1","priority");
                model.addAttribute("str2",select);
                queryWrapper.eq("priority", select);
            } else if (select.equals("已完成") || select.equals("未完成") || select.equals("待处理") || select.equals("处理中") || select.equals("已解决") || select.equals("已关闭") || select.equals("未分配")||select.equals("未处理")) {
                model.addAttribute("str1","state");
                model.addAttribute("str2",select);
                queryWrapper.eq("state", select);
            } else if (select.equals("客户报价") || select.equals("推广工单") || select.equals("最终数量") || select.equals("大批货物") || select.equals("出厂订单")) {
                model.addAttribute("str1","title");
                model.addAttribute("str2",select);
                queryWrapper.eq("title", select);
            } else if (select.equals("手动录入") || select.equals("自动录入") || select.equals("客服录入") || select.equals("顾客录入")) {
                model.addAttribute("str1","channel");
                model.addAttribute("str2",select);
                queryWrapper.eq("channel", select);
            } else {
                model.addAttribute("str1","cclist");
                model.addAttribute("str2",select);
                queryWrapper.eq("cclist", select);
            }
        }else {
            model.addAttribute("str1","null");
            model.addAttribute("str2","null");
        }
        initworkorderservice(model,current,size,queryWrapper);
        model.addAttribute("queryWrapper",queryWrapper);
        model.addAttribute("current",current);
        model.addAttribute("main","工单管理页面");
        return "workorder";
//
    }

    public void initworkorderservice(Model model, Integer current, Integer size, QueryWrapper queryWrapper)
    {

        IPage<WorkOrder> page=new Page<>(current,size);
        IPage<WorkOrder> page1 = workOrderService.page(page,queryWrapper);

        List<WorkOrder> list = page1.getRecords();
        long pagesCount = page1.getPages();


        model.addAttribute("list",list);
        model.addAttribute("pagesCount",pagesCount);
        model.addAttribute("main","工单管理页面");

    }
}

